$(document).ready(function(){
	$("#accountz").click(function(){
		$("#account-list").slideToggle();
	});
	$(".loader").hide();

	$("#add-recipients-wrap").hide();
	$('.error-r').hide();
	$('.notice').hide();

	$("#recipients-btn").click(function(){
		$("#add-recipients-wrap").fadeIn("slow");
	});
	$("#m-close").click(function(){
		$("#add-messages-wrap").fadeOut("slow");
	});
	$("#r-close").click(function(){
		$("#add-recipients-wrap").fadeOut("slow");
	});
	$('#totype-list-recipients').hide();

	$("#con-receptients input").focus(function(){ $(this).val('');
		switch($(this).attr('id')){
			case 'add-recep-fname': $(this).focusout(function(){
				if($(this).val()=='') $(this).val('First Name');
			}); break;
			case 'add-recep-lname': $(this).focusout(function(){
				if($(this).val()=='') $(this).val('Last Name');
			}); break;
			case 'add-recep-mname': $(this).focusout(function(){
				if($(this).val()=='') $(this).val('Middle Name');
			}); break;
			case 'add-recep-address':     $(this).focusout(function(){
				if($(this).val()=='') $(this).val('Address');
			}); break;
			case 'add-recep-contact':  $(this).focusout(function(){
				if($(this).val()=='') $(this).val('Contact Address');
			}); break;
			case 'add-recep-phone':  $(this).focusout(function(){
				if($(this).val()=='') $(this).val('Phone');
			}); break;
			case 'add-recep-email':  $(this).focusout(function(){
				if($(this).val()=='') $(this).val('Email');
			}); break;
		}
	});
	$("#recep-cancel").live('click',function(){
		$('#a_recipients h2').trigger('click');
	});

	$("#con-recipients input").focus(function(){ $(this).val('');

	 switch($(this).attr('id')){

	     case 'add-recep-fname': $(this).focusout(function(){
	     	if($(this).val()=='') $(this).val('First Name');
	     }); break;

	     case 'add-recep-lname': $(this).focusout(function(){
	     	 if($(this).val()=='') $(this).val('Last Name');
	     }); break;

	     case 'add-recep-mname': $(this).focusout(function(){
	     	 if($(this).val()=='') $(this).val('Middle Name');
	     }); break;

	     case 'add-recep-address':     $(this).focusout(function(){
	     	 if($(this).val()=='') $(this).val('Address');
	     }); break;
	     case 'add-recep-contact':  $(this).focusout(function(){
	     	 if($(this).val()=='') $(this).val('Contact Address');
	     }); break;
		  //Remembrance wall
		 case 'add-remwall-name':$(this).focusout(function(){
			if($(this).val()=='') $(this).val('Name of the Person');
		 });break;
	 }
	});
	$("#r-search").click(function(){
		if($(this).val() == 'Type Here...'){
			$(this).val("");
		}
	});
	$("#r-search").blur(function(){
		if($(this).val() == ''){
			$(this).val("Type Here...");
		}
	});
	//$("#add_recipients_frm").submit(function(){
	$("#recep-submit").click(function(){
		$('.error-r').html('');
		var fname = $('#add-recep-fname').attr("value");
		var lname = $('#add-recep-lname').attr("value");
		var mname = $('#add-recep-mname').attr("value");
		var address = $('#add-recep-address').attr("value");
		var phone = $('#add-recep-phone').attr("value");
		var email = $('#add-recep-email').attr("value");
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var error = 0,err_email = '',msg='',email_exist;
		var path = $('#hidden1').attr("value");
		var base = $('#hidden2').attr("value");
		if(fname == "First Name"){
			error++;
			$('#add-recep-fname').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-fname').css("border","1px solid #707070");
		}
		if(lname == "Last Name"){
			error++;
			$('#add-recep-lname').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-lname').css("border","1px solid #707070");
		}
		if(mname == "Middle Name"){
			error++;
			$('#add-recep-mname').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-mname').css("border","1px solid #707070");
		}
		if(address == "Address"){
			error++;
			$('#add-recep-address').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-address').css("border","1px solid #707070");
		}
		if(phone == "Phone"){
			error++;
			$('#add-recep-phone').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-phone').css("border","1px solid #707070");
		}
		if(email == "Email"){
			error++;
			$('#add-recep-email').css("border","1px solid #FF0000");
		}else if(!(emailReg.test(email)) ){
			err_email = "Invalid email format."
			$('#add-recep-email').css("border","1px solid #FF0000");
		}else{
			$.ajax({
				url:base+'recipients/check_email_exist',
				type:'post',
				data:'email='+email,
				success:function(data){
					if(error == 0 && err_email == '' && data == '0'){
						$('#add-recep-email').css("border","1px solid #707070");
						var form_data = $('#add_receptients_frm').serialize();
						$.ajax({
							url: path,
							data: form_data,
							type : 'POST',
							beforeSend: function(){
								$(".error-r").hide();
								$(".loader").show();
							},
							success: function(data){
								$('.loader').fadeOut(3000,function(){
									$('.error-r').html("Adding recipient is successfully save.").fadeIn(500).delay(4000).fadeOut(500, function(){
										$('#add-recep-address').css({'margin-bottom':'0'});
										$("#add-receptients-wrap").delay(1000).fadeOut("slow");
										/* for recipients side bar counter update*/
										var r_counter = parseInt($('#inp_rcounter').val());
										var ctr_min = r_counter + 1;
										$('#inp_rcounter').val(ctr_min);
										$('#rep_counter').html($('#inp_rcounter').val());
										/* end of recipient side bar counter update */
										clear_add_rep_fields();
									});
								});
							},
							complete: function(){
							}
						});
					}else{
						$('#add-recep-email').css("border","1px solid #FF0000");
						if($('.error-r').html() == ''){
							$('.error-r').html('Recipient\'s email already exist.');
						}else{
							$('.error-r').append('Recipient\'s email already exist.');
						}
						$('.error-r').show();
					}
				}
			});
		}

		$('#con-receptients input[type="text"]').css({'margin-bottom':'15px','height':'30px'})
		$('#add-recep-address').css({'margin-bottom':'0'});
		if(error > 0){msg = 'Please fill up all fields.<br/>'}
		$('.error-r').html(msg+err_email);
		$('.error-r').show();
	});
	$('#recep-edit').click(function(){
		var fname = $('#add-recep-fname').attr("value");
		var lname = $('#add-recep-lname').attr("value");
		var mname = $('#add-recep-mname').attr("value");
		var address = $('#add-recep-address').attr("value");
		var phone = $('#add-recep-phone').attr("value");
		var email = $('#add-recep-email').attr("value");
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		var error = 0,err_email = '',msg='',email_exist;
		var path = $('#hidden1').attr("value");
		var base = $('#hidden2').attr("value");
		if(fname == "First Name"){
			error++;
			$('#add-recep-fname').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-fname').css("border","1px solid #707070");
		}
		if(lname == "Last Name"){
			error++;
			$('#add-recep-lname').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-lname').css("border","1px solid #707070");
		}
		if(mname == "Middle Name"){
			error++;
			$('#add-recep-mname').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-mname').css("border","1px solid #707070");
		}
		if(address == "Address"){
			error++;
			$('#add-recep-address').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-address').css("border","1px solid #707070");
		}
		if(phone == "Phone"){
			error++;
			$('#add-recep-phone').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-phone').css("border","1px solid #707070");
		}
		if(email == "Email"){
			error++;
			$('#add-recep-email').css("border","1px solid #FF0000");
		}else if(!(emailReg.test(email)) ){
			err_email = "Invalid email format."
			$('#add-recep-email').css("border","1px solid #FF0000");
		}else{
			$('#add-recep-email').css("border","1px solid #707070");
		}
		if(error == 0 && err_email == ''){
			var path = $('#path').val();
			var form_data = $('#edit_receptients_frm').serialize();
			var html = '';
			$.ajax({
				url:path+'recipients/update_recipient',
				type:'post',
				data:form_data,
				success:function(data){
					if(data == '1'){
						html += '<label>'+$('#add-recep-fname').val()+'&nbsp;'+$('#add-recep-lname').val()+'</label>';
						html += '<p>'+$('#add-recep-address').val()+'</p>';
						html += '<p>'+$('#add-recep-phone').val()+'</p>';
						html += '<div id = "recipient-control">';
							html += '<a href = "javascript:void(0);" onclick="edit_recipient(\''+$('#edit-recep-id').val()+'\')">';
							html += '<img src = "'+path+'assets/images/edit-btn.png" alt = "" /></a> &nbsp;&nbsp;&nbsp;';
							html += '<a href = "javascript:void(0)" onclick="deleteRecipient(\''+$('#edit-recep-id').val()+'\')">';
							html += '<img src = "'+path+'assets/images/del-btn.png" alt = "" /></a>&nbsp;&nbsp;';
						html += '</div>';
						$('#recipient-context_'+$('#edit-recep-id').val()).html(html);
						$('.error-r').html('Successfully updated '+$('#add-recep-fname').val()+' info').fadeIn(500).delay(4000).fadeOut(500, function(){
							$('#ad_wrap').fadeOut(2000);
						});
					}

				}
			});
		}else{
			$('#con-receptients input[type="text"]').css({'margin-bottom':'15px','height':'30px'})
			$('#add-recep-address').css({'margin-bottom':'0'});
			if(error > 0){msg = 'Please fill up all fields.<br/>'}
			$('.error-r').html(msg+err_email);
			$('.error-r').show();
		}
		return false;
	});
	$('#recep-edit-cancel').click(function(){
		$('#ad_wrap').hide();
		return false;
	});
	$("#con-receptients input").live('focus',function(){
	 switch($(this).attr('id')){
	     case 'add-recep-fname': $(this).focusout(function(){
	     	if($(this).val()=='') $(this).val('First Name');
	     }); break;

	     case 'add-recep-lname': $(this).focusout(function(){
	     	 if($(this).val()=='') $(this).val('Last Name');
	     }); break;

	     case 'add-recep-mname': $(this).focusout(function(){
	     	 if($(this).val()=='') $(this).val('Middle Name');
	     }); break;

	     case 'add-recep-address':     $(this).focusout(function(){
	     	 if($(this).val()=='') $(this).val('Address');
	     }); break;
	     case 'add-recep-contact':  $(this).focusout(function(){
	     	 if($(this).val()=='') $(this).val('Contact Address');
	     }); break;

	 }
	});
	$('#recipient-searchsubmit').click(function(){
		var path = $('#path').val();
		var form_data = $('#frm_rec_search').serialize();
		if($('#r-search').val() != '' && $('#r-search').val() != 'Type Here...'){
			$.ajax({
				url:path+'recipients',
				type:'post',
				data:form_data,
				beforeSend: function(){
					$(".loader").show();
				},
				success:function(data){
					$(".loader").fadeOut(function(){
						$('#div_results').html(data).slideDown(2000);
					});
				}
			});
		}

		return false;
	});
	$('#btn-addpost').click(function(){
		$('#add_remwall').fadeIn(1000);
	});
	$('#rem-cancel').click(function(){
		$('#add_remwall').fadeOut(1000,function(){
			$('#cancel_flower').trigger('click');
			$('.error-r').html('');
			$('.error-r').hide();
			$('#con-recipients select,#con-recipients input[type="text"],#con-recipients textarea').css("border","1px solid #707070");
		});
	});
	$('#sel_birthday_month,#sel_death_month').change(function(){
		var base = $('#hidden1').val();
		var id = $(this).attr('id');
		$.ajax({
			url:base+'profile/get_days_per_month',
			type:'post',
			data:'month='+$(this).val()+'&id='+id,
			success:function(data){
				if(id == 'sel_birthday_month'){
					$('#sel_birthday_day').replaceWith(data);
				}else{
					$('#sel_death_day').replaceWith(data);
				}
			}
		});
	});
	$('#add-remwall-msg').focus(function(){
		$(this).val('');
		$(this).focusout(function(){
			if($(this).val()=='') $(this).val('Message');
		});
	});
	$('#add_flower').click(function(){
		var id = $(this).attr('id');
		$('.flower_gallery_wrapper').slideDown(1000,function(){
			$('#'+id).attr('id','cancel_flower');
			$('#cancel_flower .fontbb002a').html('Cancel');
			$('.slidedown_icon').replaceWith('<div class="slideup_icon fleft"></div>');
		});
	});
	$('#cancel_flower').live('click',function(){
		var id = $(this).attr('id');
		$('.flower_gallery_wrapper').slideUp(1000,function(){
			$('#file_name').val('');
			$('#flower_list li a img').css({'border':'4px solid #fff'});
			$('#'+id).attr('id','add_flower');
			$('#add_flower .fontbb002a').html('Add');
			$('.slideup_icon').replaceWith('<div class="slidedown_icon fleft"></div>');
		});
	});
	$('#flower_list li a').click(function(){
		$('#flower_list li a img').css({'border':'4px solid #fff'});
		$(this).children().css({'border':'4px solid #bf002b'});
		$('#file_name').val($(this).children().attr('alt'));
	});
	$('#rem-submit').click(function(){
		var base = $('#hidden1').val();
		var form_data = $('#add_remwall_frm').serialize();
		var error = 0;
		if($('#add-remwall-name').val() == 'Name of the Person'){
			error++;
			$('#add-remwall-name').css("border","1px solid #FF0000");
		}else{
			$('#add-remwall-name').css("border","1px solid #707070");
		}
		$("#con-recipients select option:selected").each(function () {
			if($(this).val() == ''){
				error++;
				$(this).parent().css({"border":"1px solid #FF0000"});
			}else{
				$(this).parent().css({"border":"1px solid #707070"});
			}
        });
		if($('#add-remwall-msg').val() == 'Message'){
			error++;
			$('#add-remwall-msg').css("border","1px solid #FF0000");
		}else{
			$('#add-remwall-msg').css("border","1px solid #707070");
		}
		if(error == 0){
			$.ajax({
				url:base+'/profile/save_remembrance_wall',
				type:'post',
				data:form_data,
				success:function(data){
					if(data != '1'){
						$('.error-r').html('Successfully save post.').fadeIn(2000,function(){
							$('#rem-cancel').trigger('click');
							$('.rem_wrapper').prepend(data);
						});

					}//end if
				}
			});
		}else{
			$('.error-r').html('Please fill up form correctly.').show();

		}
	});
	$('#sel_birthday_year').change(function(){
		var base = $('#hidden1').val();
		var currentTime = new Date();
		var c_year = currentTime.getFullYear();//current year
		var html = '';
		html += '<select name="sel_death_year" id="sel_death_year">';
		html += '<option value="">Year</option>';
		//"edit_recipient(\''+$('#edit-recep-id').val()+'\')"
		for(year=c_year;year>=$(this).val();year--){
			html += '<option value="'+year+'">'+year+'</option>';
		}
		html += '</select>';
		$('#sel_death_year').replaceWith(html);
	});
});

function clear_add_rep_fields(){
	$('#add-recep-fname').val('First Name');
	$('#add-recep-lname').val('Last Name');
	$('#add-recep-mname').val('Middle Name');
	$('#add-recep-address').val('Address');
	$('#add-recep-contact').val('Contact Address');
	$('#add-recep-phone').val('Phone');
	$('#add-recep-email').val('Email');
	$('.error-r').html('');
	$('.error-r').hide('');
	$('#con-receptients input[type="text"]').css({'margin-bottom':'19px','height':'35px','border':''})
	$('#add-recep-address').css({'margin-bottom':'0'});
}
function deleteRecipient(id){
	var path = $('#path').val();
	$del = confirm("Do you really want to delete this recipient?");
	if($del){
		//var dataString = 'RecipientID=' +id + '&action=deleterecipient';
		var notice = 'Recipient successfully deleted.';
		$.ajax({
			url: path+'recipients/delete_recipients',
			type: 'POST',
			data:'rec_id='+id,
			beforeSend: function(){
			  // Handle the beforeSend event
			 },
			success: function(data) {
				$('.notice').html(notice);
				$('.notice').fadeIn(2000,function(){
					$('#recipient-context_'+id).slideUp(2000,function(){
						/* for recipients side bar counter update*/
							var r_counter = parseInt($('#inp_rcounter').val());
							var ctr_min = r_counter - 1;
							$('#inp_rcounter').val(ctr_min);
							$('#rep_counter').html($('#inp_rcounter').val());
							/* end of recipient side bar counter update */
						$('.notice').fadeOut(2000);
						$('#recipient-context_'+id).remove();
					});
				});
			},
			complete: function(){
				// Handle the complete event
			}
		});
		return false;

	}
}

function edit_recipient(id){
	var path = $('#path').val();
	$.ajax({
		url:path+'recipients/edit_recipient',
		type:'post',
		data:'rec_id='+id,
		success:function(data){
			$('#rec_edit_fields').html(data);
			$('#ad_wrap').show();
		}
	});
}

function DeleteMessage(id){
	if( confirm("Do you really want to delete this message?") ){
		var baseurl = $('#baseurl').val()
		var url = baseurl+'index.php/messages/delete/'+id
		$.ajax({
			url: url,
			type: 'POST',
			data: {},
			success: function(data) {
				$('#mssg'+id).remove();
			}
		});
	}
}